<?php


namespace app\common\model;

class Admin extends Base
{
    protected $insert = ['addtime', 'password'];

    //---------------自动完成----------------------
    protected function setAddtimeAttr()
    {
        return time();
    }
    protected function setPasswordAttr($value)
    {
        return StrMD5($value);
    }
    //--------------------------------------

    //--------------获取器---------------
    public function getAddtimeTextAttr($value, $data)
    {
        return date('Y-m-d H:i', $data['addtime']);
    }
    public function getStateTextAttr($value, $data)
    {
        $array = ['0' => '禁用', '1' => '开启'];
        return $array[$data['state']];
    }
    public function getHeadimgAttr($value)
    {
        return $value ? $value : config('app_host') . '/static/admin/images/head.png';
    }

    /**
     * 登录
     */
    public function login($params)
    {
        $data = $this->where('account', $params['account'])->find();
        if (empty($data)) {
            return self::errorResult('账号不存在');
        }
        if ($data['password'] != StrMD5($params['password'])) {
            return self::errorResult('密码错误');
        }
        if ($data['state'] == 1) {
            return self::errorResult('此账号被禁用');
        }
        session('Admin', $data);
        session('RoleId', $data['role_id']);
        
        return self::successResult($data);
    }
}
